<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Models\Code;
use Curl\Curl;
use Illuminate\Support\Facades\Log;

class SendMsg extends Event implements ShouldBroadcast
{
    use SerializesModels;

    public $code;
    
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Code $code)
    {
        $this->code = $code;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        $uri = 'http://114.215.140.42:8866/sms.aspx';
        $data = array(
            'action'    => 'send',//'overage',
            'userid'    => 54,
            'account'   => '1001005',
            'password'  => 'abcd1234',
            'mobile'    => $this->code->phone,
            'content'   => $this->code->content,
            'extno'     => ''
        );
        $curl  =  new Curl();
        $curl->post($uri, $data);
        $result = $curl->rawResponse;
        $dom = new \DOMDocument();
        $dom->loadXML($result);
        $status =  $dom->getElementsByTagName('returnstatus')->item(0)->nodeValue;
        $remainpoint =  $dom->getElementsByTagName('remainpoint')->item(0)->nodeValue;
        $taskId =  $dom->getElementsByTagName('taskID')->item(0)->nodeValue;
        $succesCounts =  $dom->getElementsByTagName('successCounts')->item(0)->nodeValue;
        if($status == 'Success')
        {
            $this->code->update(['status'=>2]);
        }
        else
        {
            $this->code->update(['status'=>3]);
            Log::info(['短信发送失败'=>array('yue'=>$remainpoint,'taskid'=>$taskId,'succesCounts'=>$succesCounts)]);
        }
        return ['code.'.$this->code->id];
    }
}
